/*
 * p1561.cpp
 *
 *  Created on: 2013-5-29
 *      Author: zy
 */

#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
const int maxn = 205;
int dp[maxn][maxn], f[maxn][maxn];
int value[maxn];
vector<int> son[maxn];
int n, m;
void dfs(int u)
{
	if (son[u].size() == 0)
	{
		f[u][1] = value[u];
		return;
	}
	for (int i = 0; i < son[u].size(); i++)
	{
		int v = son[u][i];
		dfs(v);
		for (int j = m; j >= 0; j--)
			for (int k = 0; k <= j; k++)
			{
				dp[u][j] = max(dp[u][j], f[v][k] + dp[u][j - k]);
			}
	}
	for (int j = 1; j <= m; j++)
		f[u][j] = dp[u][j - 1] + value[u];
}
int main()
{
	while (scanf("%d%d", &n, &m) && n)
	{
		int x, y;
		for (int i = 0; i <= n; i++)
			son[i].clear();
		for (int i = 1; i <= n; i++)
		{
			scanf("%d%d", &x, &value[i]);
			son[x].push_back(i);
		}
		memset(dp, 0, sizeof(dp));
		memset(f, 0, sizeof(f));
		dfs(0);
		printf("%d\n", dp[0][m]);
	}
	return 0;

}
